package com.wanmao.test.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.wanmao.test.InstitutionListActivity;
import com.wanmao.test.R;
import com.wanmao.test.model.InstitutionModel;

import java.util.List;


public class InstitutionItemsGridviewAdapter extends BaseAdapter {
    private Context context;
    private List<InstitutionModel.TabsBean> tabsList;
    LayoutInflater layoutInflater;
    String fileName;

    public InstitutionItemsGridviewAdapter(Context context, String fileName, List<InstitutionModel.TabsBean> tabsList) {
        this.context = context;
        this.fileName = fileName;
        this.tabsList = tabsList;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return tabsList.size();
    }

    @Override
    public InstitutionModel.TabsBean getItem(int i) {
        return tabsList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            convertView = layoutInflater.inflate(R.layout.item_institution_grid, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.init(getItem(position));
        return convertView;
    }

    class ViewHolder {
        TextView tv_grid_item;

        public ViewHolder(View convertView) {
            tv_grid_item = (TextView)convertView.findViewById(R.id.tv_grid_item);
        }

        public void init(final InstitutionModel.TabsBean item) {
            tv_grid_item.setText(item.getStep());
            tv_grid_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null == item.getContent() || item.getContent().size() == 0)
                        return;
                    InstitutionListActivity.startActivity(context, fileName, item);
                }
            });
        }
    }
}
